package com.sunxueqi.project.system.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sunxueqi.common.constant.UserConstants;
import com.sunxueqi.common.exception.UserException;
import com.sunxueqi.framework.annotation.Log;
import com.sunxueqi.framework.annotation.VLog;
import com.sunxueqi.framework.enums.BusinessType;
import com.sunxueqi.framework.web.controller.BaseController;
import com.sunxueqi.framework.web.vo.ResponseData;
import com.sunxueqi.project.system.service.IUserService;
import com.sunxueqi.project.system.vo.User;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;

@Api(tags="用户")
@RestController
@RequestMapping("/system/user")
public class UserController extends BaseController{

	@Autowired
	private IUserService userService;
	
	/**
	 * 获取用户信息列表
	 * @param user
	 * @return
	 */
	@VLog(title = "用户信息查询")
	@PreAuthorize("@permissionService.hasPermission('system:user:list')")
	@GetMapping("/list")
	public ResponseData list(User user) {
		startPage();
		List<User> list = userService.selectUserList(user);
		return ResponseData.successTableData(list);
	}
	
	/**
	 * 新增用户
	 * @param user
	 * @return
	 */
	@PreAuthorize("@permissionService.hasPermission('system:user:add')")
	@PostMapping
	public ResponseData add(@RequestBody User user) {
		if(UserConstants.NOT_UNIQUE.equals(userService.checkUsernameUnique(user))) {
			return ResponseData.error("新增用户【" + user.getUsername() +"】失败，该账号已存在");
		} else if(UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user))) {
			return ResponseData.error("新增用户【" + user.getUsername() +"】失败，手机号码" + user.getPhone() + "已存在");
		}
		return super.getResponseDataRow(userService.addUser(user));
	}
	
	/**
	 * 修改用户信息
	 * @param user
	 * @return
	 */
	@PutMapping
	public ResponseData update(@RequestBody User user) {
		return super.getResponseDataRow(userService.updateUser(user));
	}
	
	@DeleteMapping("/{ids}")
	public ResponseData delete(@PathVariable String ids) {
		String [] idArray = StrUtil.split(ids, ",");
		return super.getResponseDataRow(userService.deleteUserByIds(idArray));
	}
	
	/**
	 * 修改用户状态
	 * @param user
	 * @return
	 */
	@PutMapping("/changeStatus")
	public ResponseData changeStatus(@RequestBody User user) {
		return getResponseDataRow(userService.changeStatus(user));
	}
	
	/**
	 * 重置密码
	 * @param user
	 * @return 
	 */
	@PutMapping("/resetPwd")
	public ResponseData resetPwd(@RequestBody User user) {
		return getResponseDataRow(userService.resetPwd(user));
	}
	
}
